package com.junzhiit.nengji.shop.dao.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.bainaeco.framework.base.dao.AbstractBaseDAO;
import com.bainaeco.framework.common.jdbc.StatementParameter;
import com.bainaeco.framework.enums.IsDelete;
import com.junzhiit.nengji.shop.bo.GoodsLabelItemsBO;
import com.junzhiit.nengji.shop.dao.GoodsLabelItemsDAO;
import com.junzhiit.nengji.shop.model.GoodsLabelItems;

@Repository
public class GoodsLabelItemsDAOImpl extends AbstractBaseDAO<GoodsLabelItems> implements GoodsLabelItemsDAO{
	
	@Override
	public List<GoodsLabelItemsBO> getAll(String goodsId){
		
		StringBuffer sql=new StringBuffer();
		sql.append(" select l.name,li.value,gli.label_id,gli.label_items_id,gli.sort from goods_label_items gli");
		sql.append(" left join label l on l.id=gli.label_id");
		sql.append(" left join label_items li on li.id=gli.label_items_id");
		sql.append(" where gli.is_delete=? ");
		sql.append(" and goods_id=? ");
		sql.append(" order by gli.sort desc");
		
		StatementParameter param=new StatementParameter();
		param.setInt(IsDelete.NO.toIntValue());
		param.setString(goodsId);
		
		return queryForList(sql.toString(), GoodsLabelItemsBO.class, param);
	}
	
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
	@Override
	public boolean deleteLabel(GoodsLabelItems goodsLabelItems){
		StringBuffer sql=new StringBuffer();
		sql.append(" delete from goods_label_items");
		sql.append(" where 1=1");
		
		StatementParameter param=new StatementParameter();
		if(StringUtils.isNotBlank(goodsLabelItems.getGoodsId())){
			sql.append(" and goods_id=?");
			param.setString(goodsLabelItems.getGoodsId());
		}
		
		if(StringUtils.isNotBlank(goodsLabelItems.getLabelId())){
			sql.append(" and label_id=?");
			param.setString(goodsLabelItems.getLabelId());
		}
		
		if(StringUtils.isNotBlank(goodsLabelItems.getLabelItemsId())){
			sql.append(" and label_items_id=?");
			param.setString(goodsLabelItems.getLabelItemsId());
		}
		
		return super.updateForBoolean(sql.toString(), param);
	}
	
}
